﻿using System;
using System.Collections.Generic;
using System.Linq;
using ZedGraph;

namespace GreenXi.Help.Curve
{
    public static class ClsJudge
    {
        public static bool CheckJudge1(PointPairList ListPress, double mX1, double mX2, double mY1, double mY2)  //左进右出
        {
            #region 填充不规则框的数据

            List<Position> position = new List<Position>();

            position.Add(new Position() { x = mX1, y = mY1 });
            position.Add(new Position() { x = mX2, y = mY1 });
            position.Add(new Position() { x = mX2, y = mY2 });
            position.Add(new Position() { x = mX1, y = mY2 });

            //不规则图像x坐标集合
            List<double> xList = position.Select(x => x.x).ToList();

            //不规则图像y坐标集合
            List<double> yList = position.Select(x => x.y).ToList();
            #endregion

            int nStep = 0;
            bool mflag = false;
            int nStart = 0;
            int nLast = 0;
            for (int i = nStep; i < ListPress.Count; i++)
            {
                if (ListPress[i].X >= mX1 && ListPress[i].X <= mX2)
                {
                    if (ListPress[i].Y >= mY1 && ListPress[i].Y <= mY2)
                    {
                        if (ListPress[i].Y >= mY1) //与Y1交叉点 
                            nStart = i;
                        if (nStart > 0 && ListPress[i].Y <= mY2)//与Y2交叉点
                            nLast = i;

                        mflag = true;
                    }
                }

            }

            if (mflag)
            {
                if (ListPress[nStart - 1].Y >= mY1 && nLast + 1 < ListPress.Count && ListPress[nLast + 1].Y <= mY2)
                {

                }
                else
                    mflag = false;
            }
            return mflag;
        }

        public static bool CheckJudge2(PointPairList ListPress, double mX1, double mX2, double mY1, double mY2) //左进上出
        {
            #region 填充不规则框的数据
            List<Position> position = new List<Position>();
            position.Add(new Position() { x = mX1, y = mY1 });
            position.Add(new Position() { x = mX2, y = mY1 });
            position.Add(new Position() { x = mX2, y = mY2 });
            position.Add(new Position() { x = mX1, y = mY2 });
            //不规则图像x坐标集合
            List<double> xList = position.Select(x => x.x).ToList();
            //不规则图像y坐标集合
            List<double> yList = position.Select(x => x.y).ToList();
            #endregion

            bool mflag = false;
            int nStep = 0;
            PointPairList ListPressTemp = new PointPairList();
            int nStart = 0;
            int nLast = 0;


            for (int i = nStep; i < ListPress.Count; i++)
            {
                if (ListPress[i].X >= mX1 && ListPress[i].X <= mX2 && ListPress[i].Y >= mY1 && ListPress[i].Y <= mY2)
                {
                    if (nStart == 0 && ListPress[i].X >= mX1)
                        nStart = i; //记录第一次进来的循环数
                    nLast = i;
                    mflag = true;

                }
            }
            if (mflag)
            {
                if (ListPress[nStart - 1].Y >= mY1 && ListPress[nStart - 1].Y <= mY2 && nLast + 1 < ListPress.Count && ListPress[nLast + 1].X <= mX2)
                {

                }
                else
                    mflag = false;

            }
            return mflag;
        }

        public static bool CheckJudge5(PointPairList ListPress, double mX1, double mX2, double mY1, double mY2) //最大位移
        {
            //判断最大x是否大于要求的值即可
            bool mflag = true;

            double MaxX = ListPress.MaxBy(x => x.X).X;
            if (MaxX > mX2)
            {
                mflag = false;
            }

            return mflag;
        }

        public static bool CheckJudge3(PointPairList ListPress, double mX1, double mX2, double mY1, double mY2) //最大压力
        {

            #region 填充不规则框的数据
            List<Position> position = new List<Position>();
            position.Add(new Position() { x = mX1, y = mY1 });
            position.Add(new Position() { x = mX2, y = mY1 });
            position.Add(new Position() { x = mX2, y = mY2 });
            position.Add(new Position() { x = mX1, y = mY2 });
            //不规则图像x坐标集合
            List<double> xList = position.Select(x => x.x).ToList();
            //不规则图像y坐标集合
            List<double> yList = position.Select(x => x.y).ToList();
            #endregion


            int nStep = 0;
            int nFirstY = 0;
            PointPairList ListPressTemp = new PointPairList();
            int nStart = 0;
            bool mflag = false;
            for (int i = nStep; i < ListPress.Count; i++)
            {
                if (ListPress[i].X >= mX1 && ListPress[i].X <= mX2 && ListPress[i].Y >= mY1 && ListPress[i].Y <= mY2)
                {
                    if (nStart == 0)
                        nStart = i; //记录第一次进来的循环数
                    mflag = true;
                    //mflag = PositionPnpoly(position.Count, xList, yList, ListPress[i].X, ListPress[i].Y);
                    //if (!mflag)
                    //{
                    //    if (!(ListPress[i].Y <= mY1))
                    //        break;
                    //}
                }

                if (ListPress[i].X > mX2) //坐标值超出为不合格
                {
                    mflag = false;
                    break;
                }
                if (nFirstY == 0) //进入框内的与Y1的交叉点 用于判定上一个点是否在X1和X2之间
                {
                    if (ListPress[i].Y >= mY1)
                        nFirstY = i;
                }
            }

            if (mflag)
            {
                if (nFirstY == 0)
                    mflag = false;
                else
                {
                    if (!(ListPress[nFirstY - 1].X >= mX1 && ListPress[nFirstY - 1].X <= mX2))  //判定上一个点是否在X1和X2之间
                        mflag = false;
                }
            }

            return mflag;
        }

        public static bool CheckJudge6(PointPairList ListPress, double mX1, double mX2, double mY1, double mY2) //最大拐点在框内
        {

            #region 填充不规则框的数据
            List<Position> position = new List<Position>();

            position.Add(new Position() { x = mX1, y = 0 });//该判定方式,需要Y1为0 便于不规则判定结果
            position.Add(new Position() { x = mX2, y = 0 });//该判定方式,需要Y1为0 便于不规则判定结果
            position.Add(new Position() { x = mX2, y = mY2 });
            position.Add(new Position() { x = mX1, y = mY2 });

            //不规则图像x坐标集合
            List<double> xList = position.Select(x => x.x).ToList();
            //不规则图像y坐标集合
            List<double> yList = position.Select(x => x.y).ToList();
            #endregion

            bool mflag = false;

            List<Position> peakPosition = new List<Position>(); //峰值集合

            for (int i = 1; i < ListPress.Count - 1; i++)
            {
                if (ListPress[i].Y >= ListPress[i - 1].Y && ListPress[i].Y >= ListPress[i + 1].Y)
                {
                    peakPosition.Add(new Position() { x = ListPress[i].X, y = ListPress[i].Y });
                }

            }
            var ylist = peakPosition.Select(x => x.y).ToList();//找出最大峰压力值
            double yPeak = ylist.Max();

            var peakPoint = peakPosition.Select(x => x).Where(x => x.y == yPeak).First(); //找出最大拐点
            if (peakPoint.x > mX1 && peakPoint.x < mX2 && peakPoint.y > mY1 && peakPoint.y < mY2) //判断最大拐点在框内
            {
                mflag = true;
            }
            return mflag;
        }
        public static bool CheckJudge7(PointPairList ListPress, double mX1, double mX2, double mY1, double mY2) //下进上出
        {

            #region 填充不规则框的数据
            List<Position> position = new List<Position>();
            position.Add(new Position() { x = mX1, y = mY1 });
            position.Add(new Position() { x = mX2, y = mY1 });
            position.Add(new Position() { x = mX2, y = mY2 });
            position.Add(new Position() { x = mX1, y = mY2 });
            //不规则图像x坐标集合
            List<double> xList = position.Select(x => x.x).ToList();
            //不规则图像y坐标集合
            List<double> yList = position.Select(x => x.y).ToList();
            #endregion

            bool mflag = false;
            int nStep = 0;
            PointPairList ListPressTemp = new PointPairList();
            int nStart = 0;
            int nLast = 0;


            for (int i = nStep; i < ListPress.Count; i++)
            {
                if (ListPress[i].X >= mX1 && ListPress[i].X <= mX2 && ListPress[i].Y >= mY1 && ListPress[i].Y <= mY2)
                {
                    if (nStart == 0 && ListPress[i].Y >= mY1)
                        nStart = i; //记录第一次进来的循环数
                    nLast = i;
                    mflag = true;
                }
            }
            if (mflag)
            {
                if (ListPress[nStart - 1].X >= mX1 && ListPress[nStart - 1].X <= mX2 && nLast + 1 < ListPress.Count && ListPress[nLast + 1].X <= mX2)
                {

                }
                else
                    mflag = false;

            }
            return mflag;
        }
        public class Position
        {
            /// <summary>
            /// 纬度
            /// </summary>
            public double x { get; set; }

            /// <summary>
            /// 经度
            /// </summary>
            public double y { get; set; }
        }
    }
}
